<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>函数防抖demo</title>
</head>
 
<body>
    <button id='btn'>按钮</button>
    <script>
        // @param fn 要被节流的函数
        // @param delay 规定时间
        function debounce(fn, delay) {
            var timer = null;
            return function() {
                //清除上一次延时器
                clearTimeout(timer);
                timer = setTimeout(function() {
                    fn.apply(this); //解决this指向问题
                }, delay)
            }
        }
        document.getElementById('btn').onclick = debounce(function() {
            console.log('点击事件被触发 ' + Date.now());
        }, 500) 
//连续点击  只执行最后一次
    </script>
</body>
</html>